﻿namespace Creek.UI.Effects
{
    using System;

    /// <summary>
    /// 	This transition animates with an exponential decay. This has a damping effect
    /// 	similar to the motion of a needle on an electomagnetically controlled dial.
    /// </summary>
    public class TransitionType_CriticalDamping : ITransitionType
    {
        #region Fields

        private readonly double m_dTransitionTime;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 	Constructor. You pass in the time that the transition 
        /// 	will take (in milliseconds).
        /// </summary>
        public TransitionType_CriticalDamping(int iTransitionTime)
        {
            if (iTransitionTime <= 0)
            {
                throw new Exception("Transition time must be greater than zero.");
            }
            this.m_dTransitionTime = iTransitionTime;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// </summary>
        public void onTimer(int iTime, out double dPercentage, out bool bCompleted)
        {
            // We find the percentage time elapsed...
            double dElapsed = iTime / this.m_dTransitionTime;
            dPercentage = (1.0 - Math.Exp(-1.0 * dElapsed * 5)) / 0.993262053;

            if (dElapsed >= 1.0)
            {
                dPercentage = 1.0;
                bCompleted = true;
            }
            else
            {
                bCompleted = false;
            }
        }

        #endregion
    }
}